import { isDev } from '@/util'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const dev = isDev()
NProgress.configure({ showSpinner: false })

function lazyLoadView(importFun: () => Promise<any>) {
  NProgress.start()
  return new Promise((resolve, reject) => {
    importFun()
      .then(resolve, reject)
      .finally(() => {
        NProgress.done()
      })
  })
}

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Index',
    redirect: dev ? '/about' : '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => lazyLoadView(() => import('@/views/Home/index.vue')),
    meta: {
      title: 'Home',
      hideNavBar: false
    }
  },
  {
    path: '/test',
    name: 'Test',
    component: () => lazyLoadView(() => import('@/views/Test/index.vue')),
    meta: {
      title: '测试',
      hideNavBar: false
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => lazyLoadView(() => import('@/views/Login/index.vue')),
    meta: {
      title: '登录',
      hideNavBar: true,
      notNeedLogin: true
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => lazyLoadView(() => import('@/views/About/index.vue')),
    meta: {
      title: '关于',
      hideNavBar: false
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router
